# app/crud/musics.py
from sqlalchemy.orm import Session
from app.models.db_models import Music
from app.schemas.music import MusicCreate

def get_music(db: Session, music_id: int):
    return db.query(Music).filter(Music.id == music_id).first()

def get_musics(db: Session, skip: int = 0, limit: int = 100):
    return db.query(Music).offset(skip).limit(limit).all()

def create_music(db: Session, music: MusicCreate):
    db_music = Music(**music.dict())
    db.add(db_music)
    db.commit()
    db.refresh(db_music)
    return db_music

def update_music(db: Session, music_id: int, music_data: dict):
    db_music = db.query(Music).filter(Music.id == music_id).first()
    if not db_music:
        return None
    
    for key, value in music_data.items():
        setattr(db_music, key, value)
    
    db.commit()
    db.refresh(db_music)
    return db_music

def delete_music(db: Session, music_id: int):
    db_music = db.query(Music).filter(Music.id == music_id).first()
    if not db_music:
        return False
    
    db.delete(db_music)
    db.commit()
    return True